﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace criaSite.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnEnviar_Click(object sender, EventArgs e)
        {
            // string siteUrl = url.Text;
            string siteTipo = DropListEvento.SelectedValue;
            SPWeb mySite = SPContext.Current.Web;
            SPList Lista = mySite.Lists["Eventos"];
            SPListItemCollection Olist = Lista.Items;
            int cont = 1;
            string siteUrl = "";
            foreach (SPListItem item in Olist)
            {
                Response.Write(item["TipoEvento"].ToString());
                if (siteTipo == item["TipoEvento"].ToString())
                {
                    cont++;
                }
            }
            if (siteTipo == "LABS")
            {
                siteUrl = "lab" + cont;
            }
            else {
                siteUrl = "oficina" + cont;
            }

            if (verificaUrl(siteUrl))
            {
                string siteTitle = titulo.Text;
                string siteDescription = descricao.Text;
             
                criaSite(siteUrl, siteTitle, siteTipo, siteDescription);
                addLista(siteUrl, siteTitle, siteTipo, siteDescription);
                Response.Redirect(siteUrl + "/sitepages/home.aspx");
            }
            else{
                lblurl.InnerText = "Já existe um portal criado com essa URL!";
            }
        }

        private bool verificaUrl(string siteUrl)
        {
            SPWeb mySite = SPContext.Current.Web;
			SPList Lista = mySite.Lists["Eventos"];
			SPListItemCollection Olist = Lista.Items;	  
			foreach(SPListItem item in Olist){
				if(item["URL"].ToString() == siteUrl){
                    return false;

                }
            }
	
            return true;
        }

        public void criaSite(string siteUrl, string siteTitle, string siteTipo, string siteDescription)
        {
                SPWeb mySite = SPContext.Current.Web;
                SPWebCollection subSites = mySite.Webs;
                SPWebTemplate template = getSiteTemplate(mySite, "Modelo_diaspora", 1046);

                mySite.AllowUnsafeUpdates = true;
                try
                {
                    subSites.Add(siteUrl, siteTitle, siteDescription, 1046, template.Name, false, false);
                }
                catch (InvalidCastException e)
                {
                    Response.Write(e.Message);
                    Response.Write(e);
                }

        }



        private SPWebTemplate getSiteTemplate(SPWeb parent, String templateName, uint localeID)
        {
            foreach (SPWebTemplate thisTemplate in parent.GetAvailableWebTemplates(localeID))
            {
                if (thisTemplate.Title.ToLower().Equals(templateName.ToLower()))
                    return thisTemplate;
            }
            return null;
        }
        private void addLista(string siteUrl, string siteTitle, string siteTipo, string siteDescription)
        {
            using (SPWeb mySite = SPContext.Current.Web)
            {
                mySite.AllowUnsafeUpdates = true;
                SPListItemCollection myList = mySite.Lists["Eventos"].Items;
                SPListItem myItem = myList.Add();

                myItem["URL"] = siteUrl;
                myItem["Title"] = siteTitle;
                myItem["TipoEvento"] = siteTipo;
                myItem["Descrição"] = siteDescription;

                myItem.Update();
                mySite.AllowUnsafeUpdates = false;

            }
        }


    }
}
